package com.smartscreen.grademanage.controller;

import com.smartscreen.common.response.ApiResponse;
import com.smartscreen.grademanage.dto.*;
import com.smartscreen.grademanage.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/grade")
public class ScoreController {
    
    @Autowired
    private ScoreService scoreService;
    
    /**
     * 批量录入/导入成绩
     */
    @PostMapping("/exams/{examId}/scores")
    public ApiResponse<ScoreImportResponse> importScores(
            @PathVariable String examId,
            @RequestBody ScoreImportRequest request) {
        ScoreImportResponse response = scoreService.importScores(examId, request);
        return ApiResponse.success("操作成功", response);
    }
    
    /**
     * 学生/家长查询个人成绩
     */
    @GetMapping("/me/scores")
    public ApiResponse<List<StudentScoreResponse>> getMyScores(
            @RequestParam(required = false) String termId,
            @RequestParam(required = false) String examId) {
        // 从Spring Security认证信息中获取当前用户的学生ID
        String studentId = getCurrentStudentId();
        
        if (studentId == null) {
            return ApiResponse.error("未找到当前用户的学生信息");
        }
        
        List<StudentScoreResponse> scores = scoreService.getStudentScores(studentId, termId, examId);
        return ApiResponse.success(scores);
    }
    
    /**
     * 教师查询班级成绩
     */
    @GetMapping("/classes/{classId}/scores")
    public ApiResponse<ClassScoreResponse> getClassScores(
            @PathVariable String classId,
            @RequestParam String examId) {
        ClassScoreResponse response = scoreService.getClassScores(classId, examId);
        return ApiResponse.success(response);
    }
    
    /**
     * 获取考试成绩分析报告
     */
    @GetMapping("/exams/{examId}/analysis")
    public ApiResponse<ExamAnalysisResponse> getExamAnalysis(
            @PathVariable String examId,
            @RequestParam String scope,
            @RequestParam String scopeId) {
        ExamAnalysisResponse response = scoreService.getExamAnalysis(examId, scope, scopeId);
        return ApiResponse.success(response);
    }
    
    /**
     * 从认证信息中获取当前学生ID
     * 根据实际的认证机制调整实现
     */
    private String getCurrentStudentId() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.isAuthenticated()) {
                // 方式1: 如果用户名就是学生ID
                String username = authentication.getName();
                if (username != null && !"anonymousUser".equals(username)) {
                    return username;
                }
                
                // 方式2: 如果需要从用户详情中获取学生ID
                // Object principal = authentication.getPrincipal();
                // if (principal instanceof UserDetails) {
                //     UserDetails userDetails = (UserDetails) principal;
                //     // 假设UserDetails实现类中有getStudentId()方法
                //     return ((CustomUserDetails) userDetails).getStudentId();
                // }
                
                // 方式3: 从认证属性中获取
                // Object studentId = authentication.getDetails();
                // if (studentId instanceof String) {
                //     return (String) studentId;
                // }
            }
        } catch (Exception e) {
            // 记录日志
            System.err.println("获取当前学生ID失败: " + e.getMessage());
        }
        
        return null;
    }
}